package jmcnet.libcommun.communs.newjmcnetds
{
	import jmcnet.libcommun.communs.exceptions.ExceptionTechnique;
	
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.remoting.RemoteObject;

	public class BlazeDSService
	{
		[Inject]
		public var channelConfig:ChannelConfiguration;
		
		public var destination:String=null;
		public var showBusyCursor:Boolean=true;
		
		private var _remoteObject:RemoteObject=null;
		
		public function BlazeDSService() {
		}
		
		public function getOperation(operationName:String):AbstractOperation {
			return getRemoteObject().getOperation(operationName);
		}
		
		public function getRemoteObject():RemoteObject {
			if (_remoteObject == null) {
				_remoteObject = new RemoteObject(destination);
				_remoteObject.showBusyCursor = showBusyCursor;
				// On verifie que channelConfig est bien initialise
				if (channelConfig == null) {
					throw new ExceptionTechnique("Erreur de configuration : ChannelConfiguration doit être injecté dans le service destination="+destination);
				}
				_remoteObject.channelSet = channelConfig.channelSet
			}
			
			return _remoteObject;
		}
		
		public function appeler(methodeName:String, args:Array=null):AsyncToken {
			var operation:AbstractOperation = getOperation(methodeName);
			if (operation == null) {
				throw new ExceptionTechnique("Erreur : operation "+methodeName+" inconnue pour service="+destination);
			}
			operation.arguments = args;
			return operation.send();
		}
	}
}